package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DishServiceImpl implements DishService {

    private final DishMapper dishMapper;

    private final DishFlavorMapper dishFlavorMapper;

    private final SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增
     * @param dishDTO
     */
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        // 1.在dish中新增菜品
        dishMapper.save(dish);
        Long dishId = dish.getId();
        // 2.在flavor中新增菜品
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if (flavorList != null && flavorList.size() > 0) {
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.saveBatch(flavorList) ;
        }
    }

    /**
     * 删除数据
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        // 1.先判断dish是否在售出
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == 1) {
                // 抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 2.判断有没有关联套餐
        List<Long> setMealIds = setmealDishMapper.getSetMealIdByDishId(ids);
        if (setMealIds != null && setMealIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        // 3.批量删除dish
        dishMapper.deletByIds(ids);

        // 4.删除其口味
        dishFlavorMapper.deleteByIds(ids);
    }

    /**
     * 修改菜品的起售与停售
     * @param status
     */
    @Override
    public void updateStatus(Long status , Long id) {
        dishMapper.updateStatus(status,id);
    }

    /**
     * 修改菜品的数据回显
     * @param id
     * @return
     */
    @Transactional
    @Override
    public DishVO SelectByDishId(Long id) {
        DishVO dishVO = new DishVO();
        // 1.查询一些基本数据
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish,dishVO);
        // 2.查询味道数据
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectById(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        // 1.修改菜品基本信息
        dishMapper.update(dish);
        // 2.先删除菜品的所有口味信息
        List<Long> dishId = Collections.singletonList(dish.getId());
        dishFlavorMapper.deleteByIds(dishId);
        // 3.再重新插入口味信息
        Long dishIds = dishDTO.getId();
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if (flavorList != null && flavorList.size() > 0) {
            flavorList.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishIds);
            });
            dishFlavorMapper.saveBatch(flavorList) ;
        }
    }
    /**
     * 根据分类id查询菜品
     *
     */
    @Override
    public List<Dish> list(Long id) {
        List<Dish> lists = dishMapper.list(id);
        return lists;
    }
}

